// pages/jidi/jidi.js
import WxParse from '../../wxParse/wxParse.js';
import util from '../../utils/util.js';
const app = getApp();
var i = 0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [{
        imgUrls: '../images/house.png',
      },
      {
        imgUrls: '../images/house.png',
      }
    ],
    isShow: false,
    imgShow: false,
    indicatorDots: true,
    interval: 3000,
    duration: 800,
    alertShow: false,
    bgShow: false,
    array: ['aaaa', 'bbb', 'ccc', 'ddd'],
    index: '',
    hv: false,
    ellipsis: true,
    showModal: false, //弹窗模块
    selectItem: ['一天一晚', '五天三晚', '月卡'],
    selectIndexAct: '', //记录当前选中
    buyNum: 1, //购买数量
    toView: 'jdts',
    hostUrl: app.globalData.hostUrl,
    one_sheng:0,
    one_zhuan:0,
    zong_zhuan:0,
    zong_sheng:0,

  },
  ellipsis: function(e) {
    var value = !this.data.ellipsis;
    this.setData({
      ellipsis: value
    })
  },
  //  isOpen:function(e){
  //    var that=this;
  //    var idx=e.currentTarget.dataset.index;
  //    var sheshiFor = that.data.sheshiFor;
  //    var hiddenaa = sheshiFor[idx].hiddena;
  //    if (btnS=='展开' && hiddenaa==true){
  //      that.setData({
  //        ["recodeList[' + idx + '].hiddena"]: !hiddenaa,
  //        ["recodeList[' + idx + '].btnS"]: '显示',

  //      })
  //    }else{
  //      that.setData({
  //        ["recodeList[' + idx + '].hiddena"]: !hiddenaa,
  //        ["recodeList[' + idx + '].btnS"]: '隐藏',

  //      })
  //    }

  //  },

  // 锚点滚动
  getStatus(e) {
    this.setData({
      status: e.currentTarget.dataset.index
    })
  },

  // 点击户型出现轮播
  imgSwiper: function() {
    this.setData({
      isShow: true
    })
  },
  alertColor: function() {
    this.setData({
      isShow: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(typeof(this.data.index));
    // console.log(typeof(this.data.selectIndexAct));

    this.getUser();

    this.jidi_info(options.id);

    this.setData({
      id: options.id,
      foot: wx.getStorageSync('foot')
    })
  },
  getUser: function() { //读取会员
    var that = this;
    var userid = wx.getStorageSync('userId');
    wx.request({
      url: app.globalData.apiUrl + 'user/getUser',
      data: {
        // opt: 'getUser',
        userId: userid,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        that.setData({
          user: res.data,


        })
      }

    })
    // console.log(that.data.canIUse)
  },
  //收藏
  shoucang: function(e) {
    var that = this;
    var userId = wx.getStorageSync('userId');
    // console.log(userId);
    // return ;
    if (wx.getStorageSync('userId') !== "") {
      wx.request({
        url: app.globalData.apiUrl + 'Index/collect',
        data: {
          // opt: 'getBanner',
          userId: userId,
          id: e.currentTarget.dataset.id,
          type: 'hotel',
        },
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res)

          if (res.data != null) {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
            that.setData({
              hv: true
            })
          }
        }
      })
    } else {

      wx.showModal({
        title: '请先登录',
        content: 'icon',
      })
    }

  },
  // 点击选择套餐
  selectClick: function(res) {
    var position = res.currentTarget.id;
    var taocan = this.data.info['cards'][position];
   this.getCardPrice(taocan['id'],this.data.user['LevelId']);
    this.setData({
      selectIndexAct: position,
      showModal: true,
      taocan: taocan,
      index: position,
      price:{
        PriceOriginal:taocan['PriceOriginal'],
        PriceVVip:taocan['PriceVVip'],
        
      },
      

    })
    WxParse.wxParse('alert', 'html', taocan['Description'], this, 5) 
    // this.getCardPrice(taocan['id'],this.data.user['LevelId'])
    this.savePrice(taocan['id'],this.data.user['LevelId'],2);//省
    this.getCommission(taocan['id'], this.data.user['LevelId'], 2);//赚

  },
  savePrice: function (info,level_id, goods_type){
    var that=this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/savePrice',
      data: {
        // opt: 'getBanner',
        card_id: info,
        level_id: level_id,
        goods_type: goods_type,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)

        if (res.data != null) {
          
          that.setData({
            one_sheng:res.data,
            zong_sheng: res.data*that.data.buyNum
          })
        }
      }
    })
  },
  getCommission: function (info,level_id, goods_type) {
    var that = this;

    wx.request({
      url: app.globalData.apiUrl + 'Hotel/getCommission1',
      data: {
        // opt: 'getBanner',
        card_id: info,
        level_id: level_id,
        goods_type: goods_type,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)

        if (res.data != null) {

          that.setData({
            one_zhuan:res.data,

            zong_zhuan: res.data*that.data.buyNum
          })
        }
      }
    })
  },
  // 购买数量 +
  addCount: function(e) {
    console.log("刚刚您点击了+++加1");
    var buyNum = this.data.buyNum;
    if (buyNum < 1000) {
      this.data.buyNum++;
    }
    this.setData({
      buyNum: this.data.buyNum,
      zong_vipprice:this.data.buyNum*this.data.vipprice,
      zong_sheng:this.data.buyNum*this.data.one_sheng,
      zong_zhuan:this.data.buyNum*this.data.one_zhuan,
    });
  },
  /* 购买数量 - */
  delCount: function(e) {
    console.log("刚刚您点击了---减1");
    var buyNum = this.data.buyNum;
    if (buyNum > 1) {
      this.data.buyNum--;
    }
    this.setData({
      buyNum: this.data.buyNum,
      zong_vipprice:this.data.buyNum*this.data.vipprice,
      zong_sheng:this.data.buyNum*this.data.one_sheng,
      zong_zhuan:this.data.buyNum*this.data.one_zhuan,
    });
  },

  //基地详情
  jidi_info: function(id) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/jidi_info',
      data: {
        // opt: 'getBanner',
        userId: wx.getStorageSync('userId'),
        id: id

      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data != null) {
          WxParse.wxParse('Content', 'html', res.data["info"].Content, that, 5);
          WxParse.wxParse('ruzhu', 'html', res.data["info"].warning, that, 5);
          for (var i = 0, len = res.data.comment.length; i < len; ++i) {
            var Description = that.delHtmlTag(res.data.comment[i].Description)
            res.data.comment[i].Description = Description;
          }
          var desc = that.delHtmlTag(res.data.info.season_text)
          that.setData({
            info: res.data,
            season_text: desc,
            taocan: res.data.cards[0],
            markers: [{
              latitude: res.data.info.location_lat,
              longitude: res.data.info.location_lng,
            }],
            price:{
              PriceOriginal:res.data.cards[0].PriceOriginal,
              PriceVVip:res.data.cards[0].PriceVVip,

            },
            vipprice:res.data.vipprice,
            zong_vipprice:res.data.vipprice,
            one_sheng:res.data.sheng,
            one_zhuan:res.data.zhuan,
            zong_sheng:res.data.sheng,
            zong_zhuan:res.data.zhuan,

          })

        }
      }
    })
  },
  delHtmlTag: function(msg) {
    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    msg = msg.replace(/[|]*\n/, '') //去除行尾空格
    msg = msg.replace(/&nbsp;/ig, ''); //去掉npsp
    return msg;
  },
  //房屋位置
  location: function(latitude, longitude) {

    wx.openLocation({
      latitude,
      longitude,
      scale: 18
    })
  },

  // 弹窗
  buycar: function() {
    this.setData({
      alertShow: true,
      bgShow: true,
    })
  },
  bghui: function() {
    this.setData({
      alertShow: false,
      bgShow: false,
    })
  },
  //套餐数量变动获取价格
  getCardPrice: function (card_id,LevelId) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'Hotel/getCardPrice',
      data: {
        card_id: card_id,
        LevelId: LevelId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        // console.log(res)
        if (res.data != null) {
         that.setData({
          zong_vipprice:res.data.price*that.data.buyNum,
           vipprice: res.data.price
        })
      }
    }
    })
  },
  //点击滚动屏幕
  jumpTo: function(e) {
    var that = this;
    let target = e.currentTarget.dataset.opt;
    console.log(target)
    this.setData({
      toView: target
    })
    console.log(this.data.toView);
  },

  buy:function(e) {
    console.log(this.data.selectIndexAct,'选中');
    return;
    if (this.data.selectIndexAct==''){
      wx.showToast({
        title: '请选择套餐',
        icon: 'none'
      })
      return;
    }
    i++;

    if (this.data.user['LevelId'] == 1) {
      wx.showToast({
        title: '请升级VIP,即可购买',
        icon: 'none'
      })
      return;
    }
    var id = e.currentTarget.dataset.id;


    if (i == 1 && that.data.user['LevelId'] < 4) {
      this.setData({
        imgShow: true
      })
    } else {
      if (!id) {
        wx.showToast({
          title: '条件必选',
          icon: 'none'
        })
        return;
      }
      wx.navigateTo({
        url: '../lvjuk_buy/lvjuk_buy?id=' + id,
      })
    }


  },
  shutDown: function() {
    this.setData({
      imgShow: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  index: function() {
    wx.reLaunch({
      url: '../index/index'
    })
  },
  // datails: function() {
  //   wx.navigateTo({
  //     url: '../datails/datails'
  //   });
  // },
  // jiankang: function() {
  //   wx.navigateTo({
  //     url: '../jiankang/jiankang'
  //   });
  // },
  // news: function() {
  //   wx.navigateTo({
  //     url: '../news/news'
  //   });
  // },
  buy:function(e){
    if (this.data.selectIndexAct == '') {
      wx.showToast({
        title: '请选择套餐',
        icon: 'none'
      })
      return;
    }
    var that=this;
    wx.navigateTo({
      url: '../jidi_order/jidi_order?id=' + that.data.taocan['id'] + "&count=" + that.data.buyNum,
    })
  },
})